package ModelLayer.FileHandlers;

import java.util.ArrayList;

import ModelLayer.Product;

/**
 * This class is an abstraction on top of BaseFileHandler, changes to the file that cointains the
 * products should only occur through the public methods of this class.
 * @author Tobias
 *
 */
public class ProductFileHandler extends BaseFileHandler {

	public ProductFileHandler(String fileName)
	{
		super(fileName);
	}
	
	public void add(Product product)
	{
		ArrayList<Product> products = getAll();
		products.add(product);
		super.saveToFile(products);
	}
	
	/**
	 * This method is used to update the stored products. All the products will be replaced with
	 * the ones in the passed ArrayList
	 * @param products
	 * The list of products to replace the existing ones.
	 */
	public void update(ArrayList<Product> products)
	{
		super.saveToFile(products);
	}
	
	@SuppressWarnings("unchecked")
	public ArrayList<Product> getAll()
	{
		if(!fileExists()){
			return new ArrayList<Product>();
		}
		
		return (ArrayList<Product>)readFromFile();
	}
	
}
